﻿using System;
using com.yoozoo.gta.Extension.UGUI.Editor;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEngine;
using UnityEngine.UI;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientation.Adapter
{
    
    [Serializable]
    public class LayoutElementAdapterConfig : AdaptBaseConfig
    {
        public bool enable = true;
        [ReadOnlyAdapter] public bool ignoreLayout;
        [ReadOnlyAdapter] public float minWidth = DEFAULT_VALUE;
        [ReadOnlyAdapter] public float minHeight = DEFAULT_VALUE;
        [ReadOnlyAdapter] public float preferredWidth = DEFAULT_VALUE;
        [ReadOnlyAdapter] public float preferredHeight = DEFAULT_VALUE;
        [ReadOnlyAdapter] public float flexibleWidth = DEFAULT_VALUE;
        [ReadOnlyAdapter] public float flexibleHeight = DEFAULT_VALUE;
        [ReadOnlyAdapter] public int layoutPriority = DEFAULT_VALUE;
        
        //重写Equals方法
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            LayoutElementAdapterConfig other = (LayoutElementAdapterConfig)obj;
            // Compare enable properties
            if (enable != other.enable)
            {
                return false;
            }
            // Compare ignoreLayout properties
            if (ignoreLayout != other.ignoreLayout)
            {
                return false;
            }
            // Compare minWidth properties
            if (minWidth != other.minWidth)
            {
                return false;
            }
            // Compare minHeight properties
            if (minHeight != other.minHeight)
            {
                return false;
            }
            // Compare preferredWidth properties
            if (preferredWidth != other.preferredWidth)
            {
                return false;
            }
            // Compare preferredHeight properties
            if (preferredHeight != other.preferredHeight)
            {
                return false;
            }
            // Compare flexibleWidth properties
            if (flexibleWidth != other.flexibleWidth)
            {
                return false;
            }
            // Compare flexibleHeight properties
            if (flexibleHeight != other.flexibleHeight)
            {
                return false;
            }
            // Compare layoutPriority properties
            if (layoutPriority != other.layoutPriority)
            {
                return false;
            }

            return true;
        }
        
        //重写GetHashCode方法
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + enable.GetHashCode();
            hash = hash * 23 + ignoreLayout.GetHashCode();
            hash = hash * 23 + minWidth.GetHashCode();
            hash = hash * 23 + minHeight.GetHashCode();
            hash = hash * 23 + preferredWidth.GetHashCode();
            hash = hash * 23 + preferredHeight.GetHashCode();
            hash = hash * 23 + flexibleWidth.GetHashCode();
            hash = hash * 23 + flexibleHeight.GetHashCode();
            hash = hash * 23 + layoutPriority.GetHashCode();
            return hash;
        }
    }
    
    [DisallowMultipleComponent]
    public class LayoutElementAdapter:AdaptBase<LayoutElementAdapterConfig,LayoutElement>
    {
        
        public override int Order => (int)AdapterExecuteOrder.Default; 
        protected override void CopyProperty(LayoutElementAdapterConfig config)
        {
            base.CopyProperty(config);
            config.enable = mComponent.enabled;
            config.ignoreLayout = mComponent.ignoreLayout;
            config.minWidth = mComponent.minWidth;
            config.minHeight = mComponent.minHeight;
            config.preferredWidth = mComponent.preferredWidth;
            config.preferredHeight = mComponent.preferredHeight;
            config.flexibleWidth = mComponent.flexibleWidth;
            config.flexibleHeight = mComponent.flexibleHeight;
            config.layoutPriority = mComponent.layoutPriority;
        }

        protected override void ApplyConfig(LayoutElementAdapterConfig config)
        {
            base.ApplyConfig(config);
            mComponent.enabled = config.enable;
            mComponent.ignoreLayout = config.ignoreLayout;
            if (config.minWidth != DEFAULT_VALUE)
            {
                mComponent.minWidth = config.minWidth;
            }
            if (config.minHeight != DEFAULT_VALUE)
            {
                mComponent.minHeight = config.minHeight;
            }
            if (config.preferredWidth != DEFAULT_VALUE)
            {
                mComponent.preferredWidth = config.preferredWidth;
            }
            if (config.preferredHeight != DEFAULT_VALUE)
            {
                mComponent.preferredHeight = config.preferredHeight;
            }
            if (config.flexibleWidth != DEFAULT_VALUE)
            {
                mComponent.flexibleWidth = config.flexibleWidth;
            }
            if (config.flexibleHeight != DEFAULT_VALUE)
            {
                mComponent.flexibleHeight = config.flexibleHeight;
            }
            if (config.layoutPriority != DEFAULT_VALUE)
            {
                mComponent.layoutPriority = config.layoutPriority;
            }
            
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                var rt = mComponent.GetComponent<RectTransform>();
                var sizeDelta = rt.sizeDelta;
                if(config.preferredHeight > -1)
                {
                    sizeDelta.y = config.preferredHeight;
                }
                if(config.preferredWidth > -1)
                {
                    sizeDelta.x = config.preferredWidth;
                }
                rt.sizeDelta = sizeDelta;
            }
            
#endif
        }
    }
    
}